<?php
/**
 *Telegram @cocolive9 2023/9/25 14:59
 */



namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms;
use think\Db;
use think\Validate;

/**
 * 首页接口
 */
class Wxapp extends Api
{


    protected $noNeedLogin = ['getopenid','wxrequest'];
    protected $noNeedRight = ['*'];


    private  $appid='wxcd87d96da1327c80';
    private $secret='686f7ad80c0bfac64f9192b358b99468';

    public function getopenid(){

        $code=$this->request->param('code');

        $appid=$this->appid;
        $key=$this->secret;

        $usrl="https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$key."&js_code=".$code."&grant_type=authorization_code";

        $response = file_get_contents($usrl);


        $this->success('',json_decode($response));
    }

    //微信登录
    public function wxrequest(){

        $openid=$this->request->param('openid');
        $nickname=$this->request->param('nickname');
        $agentid=$this->request->param('agentid');
        $username = $openid;
        $password = '111111';
        $mobile='';

        $email=substr($openid,0,10).'@vv.com';

        if (!$username || !$password) {
            $this->error(__('Invalid parameters'));
        }

        $ret = $this->auth->register($username, $password, $email, $mobile, []);
        if ($ret) {

            $user=$this->auth->getUserinfo();

            Db::name('user')->where(['id'=>$user['id']])
                ->update(['money'=>'0.00','openid'=>$openid,'agentid'=>$agentid,'nickname'=>$nickname]);


            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Sign up successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }


    }

    /**
     * 获取小程序太阳吗
     */
    public function getqrcode(){

        $txt=111222;

        $imgfile="./uploads/qrcode/".md5($txt).".png";

        if (file_exists($imgfile)){
            $this->success('','http://'.$_SERVER['HTTP_HOST'].substr($imgfile,1));
        }

        $appid=$this->appid;
        $appsecret=$this->secret;

        $access_token = $this->getaccess_token($appid, $appsecret);

        $access_token = $access_token['access_token'];

        $request_url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;

        $request_data = array(
            'scene' =>$txt,
            'page' => 'pages/index/index',
            'width' => '690',
            'is_hyaline'=>true
        );

        $img =$this->https_request($request_url, $request_data, 'json');


        $imgfile="/uploads/".md5($txt).".png";

        $filename =  ".".$imgfile;//要生成的图片名字


        $file = fopen($filename, "w");//打开文件准备写入

        fwrite($file, $img);//写入

        fclose($file);//关闭



        $this->success('图片',cdnurl($imgfile,true));

    }




    public function getaccess_token($appid, $appsecret)  {

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, 0);
        // 关闭https验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $output = curl_exec($ch);
        curl_close($ch);

        return json_decode($output, true);


        // close curl resource to free up system resources
    }

    public function https_request($url = '', $param = [], $contentType = ''){
        $ch = curl_init();

        // 请求地址
        curl_setopt($ch, CURLOPT_URL, $url);

        // 请求参数类型
        $param = $contentType == 'json' ? json_encode($param) : $param;

        // 关闭https验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        // post提交
        if ($param) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        }

        // 返回的数据是否自动显示
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 执行并接收响应结果
        $output = curl_exec($ch);

        // 关闭curl
        curl_close($ch);

        return $output !== false ? $output : false;
    }





}